package com.gxa.zhonglong.controller;

import com.gxa.zhonglong.common.JsonResult;
import com.gxa.zhonglong.exception.BusinessException;
import com.gxa.zhonglong.pojo.Storage;
import com.gxa.zhonglong.service.StorageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @auther JJuventus
 * @date 2021/10/29 15:17
 */

@RestController
@RequestMapping(value = "/storage")
@CrossOrigin
@Api(value = "库存统计")
@Transactional(rollbackFor = Throwable.class)
public class StroageController {

    @Autowired
    private StorageService storageService = null;

    @RequestMapping(value = "/getList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storageJewelryCode",value = "商品编码",dataType = "string"),
            @ApiImplicitParam(name = "storageJewelryName",value = "商品名称",dataType = "string"),
            @ApiImplicitParam(name = "storageStore",value = "门店名称",dataType = "string"),
            @ApiImplicitParam(name = "page",value = "页数",dataType = "int",required = true),
            @ApiImplicitParam(name = "size",value = "每页记录数",dataType = "int",required = true)
    })
    public Map select(@RequestParam("storageJewelryCode") String storageJewelryCode,@RequestParam("storageJewelryName") String storageJewelryName,@RequestParam("storageStore") String storageStore,@RequestParam("page") Integer page,@RequestParam("size") Integer size){
        List<Storage> storages = storageService.select(storageJewelryCode, storageJewelryName, storageStore, page, size);
        List<Storage> all = storageService.selectAll(storageJewelryCode, storageJewelryName, storageStore);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.put("total",all.size());
        jsonResult.setData(storages);
        return jsonResult.getDataMap();
    }

    @RequestMapping(value = "/update")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storage",value = "修改的库存对象",dataType = "body",required = true),
    })
    public Map update(@RequestBody Storage storage) throws BusinessException {
        storageService.update(storage);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }

    @RequestMapping(value = "/add")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storage",value = "添加的库存对象",dataType = "body",required = true),
    })
    public Map add(@RequestBody Storage storage){
        storageService.add(storage);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }
}
